package com.kuwei.service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.kuwei.utils.CommonUtils;
import com.kuwei.utils.Constants;
import com.kuwei.utils.ServiceConstants;

/**
 * userService
 * @author zyc
 *
 */
@Service("userService")
public class UserService extends CommonService {
	
	/**
	 * 查询用户
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public List<Map<String, Object>> queryUserList(Object map) throws Exception{
		try{
			String service_code ="USER.QUERY_USER";
			return commonQry(service_code,map);
		}catch(Exception e){
			throw new Exception(this.getClass().toString(),e);
		}
	}
	
	@SuppressWarnings("finally")
	public Map<String, Object> updateUser(Map<String, Object> map) throws Exception {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try{
			Map<String,Object> reqMap = new HashMap<String,Object>();
			if("modify".equals(map.get("OPERTYPE"))&&!CommonUtils.isEmpty(map.get("ID_CARD_NO"))){
				reqMap.put("ID_CARD_NO", map.get("ID_CARD_NO"));
				List<Map<String,Object>> list = commonQry(ServiceConstants.QUERY_USER_LIST,reqMap);
				if(!CommonUtils.isEmpty(list)&&list.size()>0){
					resultMap.put("rtnCode", Constants.EXIST);
					resultMap.put("rtnMsg", "该身份证号已被注册，请修改身份证号！");
				} else{
					commonUpdate("USER.UPDATE_USER",map);
					resultMap.put("rtnCode", Constants.SUCCESS);
					resultMap.put("rtnMsg", "更新用户信息成功！");
				}
			} else{
				commonUpdate("USER.UPDATE_USER",map);
				resultMap.put("rtnCode", Constants.SUCCESS);
				resultMap.put("rtnMsg", "更新用户信息成功！");
			}
		}catch(Exception e){
			resultMap.put("rtnCode", Constants.ERROR);
			resultMap.put("rtnMsg", "更新用户信息失败！");
			throw new Exception(this.getClass().toString()+e,e);
		} finally{
			return resultMap;
		}
	}
	
	@SuppressWarnings("finally")
	public Map<String, Object> insertUser(Map<String, Object> map) throws Exception {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try{
			Map<String,Object> reqMap = new HashMap<String,Object>();
			reqMap.put("ID_CARD_NO", map.get("ID_CARD_NO"));
			List<Map<String,Object>> list = commonQry(ServiceConstants.QUERY_USER_LIST,reqMap);
			reqMap = new HashMap<String,Object>();
			reqMap.put("USER_NAME", map.get("USER_NAME"));
			List<Map<String,Object>> list2 = commonQry(ServiceConstants.QUERY_USER_LIST,reqMap);
			if(!CommonUtils.isEmpty(list)&&list.size()>0){
				resultMap.put("rtnCode", Constants.EXIST);
				resultMap.put("rtnMsg", "该身份证号已被注册，请修改身份证号！");
			} else if(!CommonUtils.isEmpty(list2)&&list2.size()>0){
				resultMap.put("rtnCode", Constants.EXIST);
				resultMap.put("rtnMsg", "该用户账号已被注册，请用户账号！");
			} else{
				commonInsert("USER.INSER_USER",map);
				resultMap.put("rtnCode", Constants.SUCCESS);
				resultMap.put("rtnMsg", "新增用户信息成功！");
			}
		}catch(Exception e){
			resultMap.put("rtnCode", Constants.ERROR);
			resultMap.put("rtnMsg", "新增用户信息失败！");
			throw new Exception(this.getClass().toString()+e,e);
		} finally{
			return resultMap;
		}
	}
	
	public int deleteTest(Map<String, Object> map) throws Exception {
		try{
			String service_code ="TEST.DELETETEST";
			return commonDelete(service_code,map);
		}catch(Exception e){
			throw new Exception(this.getClass().toString(),e);
		}
	}
}
